<!DOCTYPE html>
<html lang="en">
<head xmlns:th="http://www.thymeleaf.org">
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <script src="/js/bootstrap.min.js"></script>
    <script src="/js/jquery-3.6.0.min.js"></script>
</head>
<body>
<div class="container-fluid">
    <h1>学生信息</h1>
    <!-- Button trigger modal -->
    <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal" onclick="preAdd();">
        新增学生
    </button>



    <div class="bs-example" data-example-id="bordered-table">
        <table class="table table-bordered">
            <thead>
            <tr>
                <th>#</th>
                <th>姓名</th>
                <th>性别</th>
                <th>成绩</th>
                <th>学号</th>
                <th style="width:100px;">操作</th>
            </tr>
            </thead>
            <tbody id="studentTb">

            </tbody>
        </table>
        <form class="form-Name">
            <div class="form-group">
                <label for="name1">Name</label>
                <input type="text"class="form-control" id="name1" placeholder="请输入名字"name="name1"/>
            </div>
            <button type="submit"class="btnbtn-defaultbtn-lgactive" onclick="return search();">查询</button>
        </form>
    </div>
</div>


<div class="modal fade" tabindex="-1" role="dialog" id="myModal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">学生信息</h4>
            </div>
            <div class="modal-body">

                <form id="fromStudent">
                    <input hidden="hidden" id="id" name="id"/>
                    <div class="form-group">
                        <label for="name">姓名</label>
                        <input type="text" class="form-control" id="name"  name="name" placeholder="姓名">
                    </div>
                    <div class="form-group">
                        <label for="password">密码</label>
                        <input type="password" class="form-control" id="password"  name="password" placeholder="密码">
                    </div>
                    <div class="form-group">
                        <label for="sex">性别</label>
                        <select class="form-control" id="sex" name="sex">
                            <option value="男">男</option>
                            <option value="女">女</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="number">学号</label>
                        <input type="text" class="form-control" id="number"  name="number" placeholder="学号">
                    </div>
                    <div class="form-group">
                        <label for="score">成绩</label>
                        <input type="text" class="form-control" id="score"  name="score" placeholder="成绩">
                    </div>
                </form>
                <!--表单结束-->
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="saveStudent();">保存</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->



<script src="/js/bootstrap.min.js"></script>

<script type="text/javascript" >
    function loadTable(){
        $.ajax({
            url:'/webapi/students/list'
        }).done(function (rs){
            var len=rs.length;
            var html="";
            for(var i=0;i<len;i++)
            {
                var item=rs[i];
                html+="<tr>"
                    +"<td>"+item.id+"</td>"
                    +"<td>"+item.name+"</td>"
                    +"<td>"+item.sex+"</td>"
                    +"<td>"+item.score+"</td>"
                    +"<td>"+item.number+"</td>"
                    +"<td><a href='#' onclick='editStudent("+item.id+");'>编辑</a>  " +
                    "<a href='#' onclick='deleteStudent("+item.id+")'>删除</a></td>"
                    +"</tr>"
            }
            $("#studentTb").html(html);
        })
    }
    //新增或者是更新
    function saveStudent() {

        var data=$('#fromStudent').serialize();

        var id=$('#id').val();

        if (id<1){
            //是新增
            $.ajax({
                url:"/webapi/students/insert",
                method:"post",
                data:data
            }).done(function () {

                loadTable();
                $('#myModal').modal('hide')

            })
        }else{
            //是更新
            $.ajax({
                url:"/webapi/students/update",
                method:"put",
                data:data
            }).done(function () {

                loadTable();
                $('#myModal').modal('hide')

            })
        }


    }

    function editStudent(id) {
        $('#myModal').modal('show')
        $.ajax({
            url:'/webapi/students/get/'+id
        }).done(function (rs){

            $("#id").val(rs.id);
            $("#name").val(rs.name);
            $("#sex").val(rs.sex);
            $("#password").val("");
            $("#score").val(rs.score);
            $("#number").val(rs.number);
        })

    }

    function preAdd() {
        $("#id").val(0);//提示新增
    }

    function deleteStudent(id) {
        if(confirm("你是否真的要删除吗？别后悔哟！")){

            $.ajax({
                url:"/webapi/students/delete/"+id,

                method:"delete"
            }).done(function ()
            {
                loadTable();
            })
        }
    }

    function search(){
        var name=$("#name1").val();
        console.log("name:"+name)
        getdata(name);
        return false;
    }
    function getdata(name){
        var url="/webapi/students/findByName";
        if(name!=null && name!=""){
            url +="?name="+name;
        }
        $.ajax({
            url:url
        }).done(function (rs) {
            var len = rs.length;
            var htmlStr = "";
            for (var i = 0; i < len; i++) {
                var item = rs[i];
                htmlStr+="<tr>"
                    +"<td>"+item.id+"</td>"
                    +"<td>"+item.name+"</td>"
                    +"<td>"+item.sex+"</td>"
                    +"<td>"+item.score+"</td>"
                    +"<td>"+item.number+"</td>"
                    +"<td><a href='#' onclick='editStudent("+item.id+");'>编辑</a>  " +
                    "<a href='#' onclick='deleteStudent("+item.id+")'>删除</a></td>"
                    +"</tr>"
            }
            $("#studentTb").html(htmlStr);
        })
    }
    $(function(){
        loadTable();
    });

</script>

</body>
</html>